package com.isyscore.iios.appdemo.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.isyscore.device.common.model.RespDTO;
import com.isyscore.iios.appdemo.dto.TagDTO;
import com.isyscore.iios.appdemo.service.TagService;
import com.isyscore.os.permission.exception.ErrorCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 该案例演示了如何在业务代码中调用IDPT的接口以及消息订阅，实现一些基本操作
 */
@Log4j2
@RestController
@Api(tags = "点位")
@RequestMapping("${app.apiPrefix:}/tag")
public class TagController {
   @Autowired
   private TagService tagService;

    //访问点位的分页列表
    @ApiOperation("分页查询点位列表")
    @GetMapping("page")
    public RespDTO<IPage<TagDTO>> page(@RequestParam(value = "current") Integer current,@RequestParam(value = "size") Integer size) {
        //这里只使用分页的参数,更多的参数阅读openAPi说明文档。
        Map<String, Object[]> queryParams = new HashMap<>();
        queryParams.put("current", new Object[]{current});
        queryParams.put("size", new Object[]{size});
        try {
            return RespDTO.onSuc(tagService.page(queryParams));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     *  订阅该点位的上报消息并输出到控制台
     * @param tagGroupId 点位组ID
     * @param gatewayId 网关ID
     * @param tagId 点位ID
     * @param username 有权限的用户的账号
     * @param password 有权限的用户的密码
     * @return
     */
    @ApiOperation("订阅点位数据")
    @GetMapping("subTagData")
    public RespDTO subTagData(@RequestParam(value = "tagGroupId") String tagGroupId, @RequestParam("gatewayId") String gatewayId, @RequestParam("tagId") String tagId, @RequestParam("username") String username, @RequestParam("password") String password){
        try {
            tagService.subTagData(tagGroupId, gatewayId, tagId, username, password);
        } catch (Exception e) {
            return RespDTO.onFail(ErrorCode.FAIL.getCode(),e.getMessage());
        }
        return RespDTO.onSuc();
    }
}
